package com.csx.saas.platform.auth.controller;

import com.csx.basic.annotation.user.LoginUser;
import com.csx.basic.base.R;
import com.csx.basic.dozer.DozerUtils;
import com.csx.basic.security.model.SysUser;
import com.csx.basic.utils.TreeUtil;
import com.csx.saas.platform.auth.dto.VueRouter;
import com.csx.saas.platform.system.entity.SystemMenu;
import com.csx.saas.platform.system.service.SystemMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

import static com.csx.saas.common.constant.SwaggerConstants.DATA_TYPE_LONG;
import static com.csx.saas.common.constant.SwaggerConstants.PARAM_TYPE_QUERY;

/**
 * 资源 角色 菜单
 *
 * @author ChenSongXin
 * @date 2021/2/20 15:14
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api(value = "AuthMenuResource", tags = "资源")
public class AuthMenuResourceController {

    private final DozerUtils dozer;
    private final SystemMenuService systemMenuService;


    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = DATA_TYPE_LONG, paramType = PARAM_TYPE_QUERY),
    })
    @ApiOperation(value = "查询用户可用的所有菜单路由树", notes = "查询用户可用的所有菜单路由树")
    @GetMapping("/menu/router")
    public R<List<VueRouter>> myRouter(@RequestParam(value = "userId", required = false) Long userId,
                                       @ApiIgnore @LoginUser SysUser sysUser) {
        if (userId == null || userId <= 0) {
            userId = sysUser.getId();
        }
        List<SystemMenu> list = systemMenuService.findVisibleMenu(userId);
        List<VueRouter> treeList = dozer.mapList(list, VueRouter.class);
        return R.success(TreeUtil.buildTree(treeList));
    }
}
